<template>
  <div class="login-container">
    <div class="login-form">
      <h2>登录系统</h2>
      <div class="form-item">
        <label>用户名</label>
        <input type="text" v-model="loginForm.username" placeholder="请输入用户名" />
      </div>
      <div class="form-item">
        <label>密码</label>
        <input type="password" v-model="loginForm.password" placeholder="请输入密码" />
      </div>
      <div v-if="errorMsg" class="error-message">
        {{ errorMsg }}
      </div>
      <div class="btn-group">
        <button @click="login" :disabled="loading">{{ loading ? '登录中...' : '登录' }}</button>
        <button @click="reset">重置</button>
      </div>
    </div>
  </div>
</template>

<script>
import { useAuthStore } from '@/stores/auth'

export default {
  name: 'LoginView',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      errorMsg: '',
      loading: false
    }
  },
  created() {
    // 检查本地存储中是否有token
    const token = localStorage.getItem('token')
    if (token) {
      this.$router.push('/')
    }
  },
  methods: {
    async login() {
      if (!this.loginForm.username) {
        this.errorMsg = '请输入用户名'
        return
      }
      if (!this.loginForm.password) {
        this.errorMsg = '请输入密码'
        return
      }
      
      this.errorMsg = ''
      this.loading = true
      
      try {
        // 使用Pinia store的登录方法
        const authStore = useAuthStore()
        const result = await authStore.login(this.loginForm)
        
        if (result.success) {
          this.$router.push('/')
        } else {
          this.errorMsg = result.message || '登录失败，请检查用户名和密码'
        }
      } catch (error) {
        this.errorMsg = '登录失败，请稍后重试'
      } finally {
        this.loading = false
      }
    },
    reset() {
      this.loginForm.username = ''
      this.loginForm.password = ''
      this.errorMsg = ''
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-form {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.form-item {
  margin-bottom: 25px;
}

label {
  display: block;
  margin-bottom: 10px;
  color: #606266;
  font-size: 14px;
}

input {
  width: 100%;
  height: 40px;
  padding: 0 15px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s;
}

input:focus {
  outline: none;
  border-color: #409eff;
}

.error-message {
  color: #f56c6c;
  font-size: 12px;
  margin-bottom: 15px;
}

.btn-group {
  display: flex;
  justify-content: center;
  gap: 20px;
}

button {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: opacity 0.3s;
}

button:first-child {
  background-color: #409eff;
  color: #fff;
}

button:first-child:hover:not(:disabled) {
  opacity: 0.8;
}

button:first-child:disabled {
  background-color: #c0c4cc;
  cursor: not-allowed;
}

button:last-child {
  background-color: #909399;
  color: #fff;
}

button:last-child:hover {
  opacity: 0.8;
}
</style>